package com.extension.company.view.my.address;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.extension.company.Constant;
import com.extension.company.admin.R;
import com.extension.company.common.permission.PermissionsActivity;
import com.extension.company.common.permission.PermissionsUtil;
import com.extension.company.common.mvp.BaseTitleActivity;
import com.extension.company.common.utils.ToastUtil;
import com.extension.company.common.widget.dialog.LoadingDialog;
import com.extension.company.contract.AddressListAddContract;
import com.extension.company.model.entity.AddressListData;
import com.extension.company.presenter.my.AddressListAddPresenter;
import com.extension.company.common.widget.popuwindow.PwAddressListAddPopWindow;

import butterknife.BindView;

/**
 * 功能：添加收货地址
 */
public class AddAddressActivity extends BaseTitleActivity<AddressListAddContract.View, AddressListAddPresenter>
		implements AddressListAddContract.View, View.OnClickListener, CompoundButton.OnCheckedChangeListener {

	@BindView(R.id.tv_address)
	TextView tv_address;
	@BindView(R.id.btn_add)
	Button btnAdd;
	@BindView(R.id.consignee_address)
	EditText consigneeAddress;
	@BindView(R.id.consignee_mobile)
	EditText consigneeMobile;
	@BindView(R.id.consignee_name)
	EditText consigneeName;
	@BindView(R.id.address_default)
	CheckBox checkBox;

	private static final int REQUEST_PERMISSION = 0;

	private PwAddressListAddPopWindow pwAddressListAdd;      //省市区的dialog
	private String mProviceId, mCityId, mCountryId; // 省市区
	private String defaultAddress = "0"; // 0 非默认地址，1:默认的地址
	private String id; // 编辑收货地址的item的id
	private int type = 0;  // 0 新增 1 编辑
	private LoadingDialog loadingDialog;

	public static void startAddressListAdd(Activity activity) {
		Intent intent = new Intent(activity, AddAddressActivity.class);
		activity.startActivity(intent);
	}

	@Override
	protected void initData() {
		setTitle("添加收货地址");
		pwAddressListAdd = new PwAddressListAddPopWindow(this);
		AddressListData.DataBean.AddressBean addressInfomation = getIntent().getParcelableExtra("addressInfo");

		if (null != addressInfomation) {
			consigneeName.setText(addressInfomation.getName());
			consigneeMobile.setText(addressInfomation.getPhone());
			tv_address.setText(addressInfomation.getPname() + addressInfomation.getCname() + addressInfomation.getRname());
			consigneeAddress.setText(addressInfomation.getDetail());
			checkBox.setChecked(!"0".equals(addressInfomation.getIs_default())); // 0：1 不选中：选中

			setTitle("修改收获地址");
			mProviceId = String.valueOf(addressInfomation.getProvince_id());
			mCityId = String.valueOf(addressInfomation.getCity_id());
			mCountryId = String.valueOf(addressInfomation.getRegion_id());
			id = String.valueOf(addressInfomation.getId());
			defaultAddress = addressInfomation.getIs_default();
			type = 1;
		}
	}

	@Override
	protected void initListener() {
		tv_address.setOnClickListener(this);
		btnAdd.setOnClickListener(this);
		checkBox.setOnCheckedChangeListener(this);
		setOnPwAddressListAddListener();
	}

	@Override
	public int getMainLayout() {
		return R.layout.addresslist_add;
	}

	@Override
	public AddressListAddContract.View createView() {
		return this;
	}

	@Override
	public AddressListAddPresenter createPresenter() {
		return new AddressListAddPresenter();
	}

	@Override
	public void showLoading() {
		if (null == loadingDialog) {
			loadingDialog = new LoadingDialog(this);
		}
		loadingDialog.show();
	}

	@Override
	public void hideLoading() {
		if (null != loadingDialog && loadingDialog.isShowing()) {
			loadingDialog.dismiss();
		}
	}

	@Override
	public void onError(Throwable throwable) {

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (null != loadingDialog) {
			loadingDialog.dismiss();
			loadingDialog = null;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.tv_address:
				boolean b = PermissionsUtil.openPermissions(this, PermissionsUtil.WRITE_READ_PERMISSIONS, REQUEST_PERMISSION);
				if (b) {
					pwAddressListAdd.show();
				}
				break;
			case R.id.btn_add:
				switch (type) {
					case 0:
						presenter.saveAddress(  // 发送新增网路请求
								Constant.SIGN,
								Constant.TOKEN,
								consigneeName.getText().toString().trim(),
								consigneeAddress.getText().toString().trim(),
								mProviceId, mCityId, mCountryId,
								consigneeMobile.getText().toString().trim(),
								defaultAddress);
						break;
					case 1:
						presenter.editAddress(  // 发送修改网路请求
								Constant.SIGN,
								Constant.TOKEN,
								id,
								consigneeName.getText().toString().trim(),
								consigneeAddress.getText().toString().trim(),
								mProviceId, mCityId, mCountryId,
								consigneeMobile.getText().toString().trim(),
								defaultAddress);
						break;
					default:
						break;
				}

				break;
			default:
				break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 0) {
			if (resultCode == PermissionsActivity.PERMISSIONS_DENIED) {
				ToastUtil.showToast(this, "你已经拒绝了权限");
			} else {
				pwAddressListAdd.show();
			}
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // 设置默认地址
		if (isChecked) {
			defaultAddress = "1";
		} else {
			defaultAddress = "0";
		}
	}

	@Override
	public void addAddressBackSuccess(String msg) {  // 添加地址的回调
		if (!TextUtils.isEmpty(msg)) {
			ToastUtil.showToast(this, msg);
			finish();
		}
	}

	@Override
	public void addAddressBackFaile(String msg) { // 添加失败
		if (!TextUtils.isEmpty(msg)) {
			ToastUtil.showToast(this, msg);
		}
	}

	/**
	 * 省市区dialog的监听
	 */
	private void setOnPwAddressListAddListener() {
		pwAddressListAdd.setOnPwAddressListAddListener(new PwAddressListAddPopWindow.OnPwAddressListAddListener() {
			@Override
			public void pwAddressListAdd(String provice, String city, String county) {
				tv_address.setText(provice + city + county);
			}

			@Override
			public void pwAddressListAddId(String proviceId, String cityId, String countyId) {  // 城市id的回调
				Log.d("zwk", "proviceId" + proviceId + "cityId" + cityId + "countyId" + cityId);
				mProviceId = proviceId;
				mCityId = cityId;
				mCountryId = countyId;
			}
		});
	}

}
